﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumTracker.Models;
using ScrumTracker.Helpers;
using System.Web.Script.Serialization;

namespace ScrumTracker.Controllers
{
    public class StoriesController : Controller
    {
        //
        // GET: /Stories/

        public ActionResult New(Story model)
        {
            ModelState.Clear();
            model.Priority = 3;
            model.Effort = 50;
            ViewData.Model = model;
            return View();
        }

        [HttpPost]
        public ActionResult Create(Story model)
        {
            model.IssueType = (int)IssueType.Story;
            model.CreatedBy = SessionHelper.CurrentUser.UserId;
            model.CreatedAt = DateTime.UtcNow;
            if (ModelState.IsValid)
            {
                var context = ScrumTrackerEntities.Instance;
                context.Issues.AddObject(model);
                context.SaveChanges();
                TempData["notice"] = "Story successfully added!";
                return RedirectToAction("Get", "Stories", new { id = model.IssueId });
            }
            return View("New");
        }

        public ActionResult Get(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            ViewData.Model = context.Issues.SingleOrDefault(e => e.IssueId == id) as Story;
            return View();
        }

        public ActionResult Edit(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var story = context.Issues.SingleOrDefault(e => e.IssueId == id) as Story;
            ViewData.Model = story;
            var tasks = story.Tasks;
            JavaScriptSerializer jss = new JavaScriptSerializer();
            ViewBag.Tasks = jss.Serialize(tasks.Select(a => new {IssueId = a.IssueId }).ToList());
            return View();
        }

        [HttpPost]
        public ActionResult Update(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var entity = context.Issues.SingleOrDefault(e => e.IssueId == id) as Story;
            if (TryUpdateModel<Story>(entity))
            {
                context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                context.SaveChanges();
                if (Request.AcceptTypes.Contains("application/json"))
                {
                    return Json(new { success = true });
                }
                else
                {
                    TempData["notice"] = "Story successfully edited!";
                    return RedirectToAction("Get", "Stories", new { id = entity.IssueId });
                }
            }
            if (Request.AcceptTypes.Contains("application/json"))
            {
                return Json(new { success = false });
            }
            else
            {
                ViewData.Model = entity;
                return View("Edit");
            }
        }

        //[HttpPost]
        //public ActionResult UpdateSprintId(int id)//, Story model)
        //{
        //    var context = ScrumTrackerEntities.Instance;
        //    var entity = context.Issues.SingleOrDefault(e => e.IssueId == id) as Story;
        //    if (TryUpdateModel(entity))
        //    {
        //        context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
        //        context.SaveChanges();
        //        return Json(new { success = true });
        //    }
        //    return Json(new { success = false });
        //}
    }
}
